import requests

from bs4 import BeautifulSoup

import time

f=open('file/kugou.txt','a+')

def get_soup(url):
    headers={'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    response=requests.get(url,headers=headers)
    soup=BeautifulSoup(response.text)
    return soup

def get_info(info_url):
    info_soup=get_soup(info_url)

    ranks=info_soup.select('span.pc_temp_num')
    titles=info_soup.select('#rankWrap >div > ul > li > a')
    times=info_soup.select('span.pc_temp_time')
    for rank,title,time in zip(ranks,titles,times):
        data={'rank':rank.get_text().strip(),
             'title':title.get_text().strip(),
             'time':time.get_text().strip(),
             }
        print(data)
        f.write(str(data) +'\n')

if __name__=='__main__':
    url='http://www.kugou.com/yy/rank/home/{}-8888.html'
    urls=[url.format(str(i)) for i in range(1,24)]
    for singleurl in urls:
        get_info(singleurl)